# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: shell_shutil.py 
@time: 2019/8/1 上午11:13
@note:
拷贝文件
"""
import shutil
import os
'拷贝文件 cp src dst'
src = os.path.join(os.getcwd(), 'file1.txt')
dst = os.path.join(os.getcwd(), 'file2.txt')
shutil.copy(src, dst)

'拷贝文件保留源文件的属性, cp -p src dst'
shutil.copy2(src, dst)

'拷贝带目录的文件, cp -R src dst'
src = os.getcwd()
dst = os.path.join('/opt', 'dst')
shutil.copytree(src, dst)
'保留被复制目录中的符号链接'
shutil.copytree(src, dst, symlinks=True)
'忽略不需要拷贝的文件，两种方法'
def ignore_pyc_files(dirname, filenames):
    return [name for name in filenames if name.endswith('.pyc')]
shutil.copytree(src, dst, ignore=ignore_pyc_files)
'第二种方法直接用内置函数'
shutil.copytree(src, dst, ignore=shutil.ignore_patterns('*-', '*.pyc'))
'拷贝过程中异常处理'
try:
    shutil.copytree(src, dst)
except shutil.Error as e:
    for src, dst, msg in e.args[0]:
        print(dst, src, msg)

'移动文件或目录'
shutil.move(src, dst)

'可归档的模式'
shutil.get_archive_formats()
# [('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), ('tar', 'uncompressed tar file'), ('xztar', "xz'ed tar-file"), ('zip', 'ZIP file')]
'归档文档'
shutil.make_archive('py3', 'zip', 'pydir')
"""
'py3':归档名称
'zip':归档格式
'pydir':要归档的目录名或者路径
"""
'解压归档'
shutil.unpack_archive('py3.zip')
'解压归档格式'
shutil.get_unpack_formats()
